﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using ggprot;

namespace GGTest
{
    public partial class Form1 : Form
    {
        private GaduProtocol protocol;
        private List<Chat> openedChats;
        private Log log;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            Login login = new Login();
            if (login.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                this.Close();
                return;
            }

            protocol = login.Protocol;
            protocol.MessageReceived += new MessageReceivedEventHandler(protocol_MessageReceived);
            protocol.MultiLogin += new MultiLoginEventHandler(protocol_MultiLogin);
            protocol.Disconnected += new EventHandler(protocol_Disconnected);
            openedChats = new List<Chat>();

            this.Invoke(new Action(delegate()
                {
                    log = new Log(protocol);
                    log.Show();
                    log.Location = new Point(this.Left + this.Width, this.Top);
                }));

            listBox1.Items.Add(5260413);
        }

        void protocol_Disconnected(object sender, EventArgs e)
        {
            MessageBox.Show("Zostałeś rozłączony");
            Invoke(new Action(delegate()
                {
                    this.Close();
                }));
        }

        void protocol_MultiLogin(object sender, MultiLoginEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Ktoś zalogował się na ten numer:");
            for (int i = 0; i < e.Inf.Count; i++)
            {
                GGMultiloginItem item = e.Inf.Items[i];
                sb.AppendLine(string.Format("#{0} - IP: {1} - Klient: {2}", i, new IPAddress(item.LoginIp), item.Name));
            }

            log.AppendLog(sb.ToString());
        }

        void protocol_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            this.Invoke(new Action(delegate()
            {
                if (openedChats.Where(t => t.Uin == e.Uin).FirstOrDefault() != null)
                    return;

                Chat chat = new Chat(e.Uin, protocol);
                chat.Show();
                chat.AppendMessage(e.Uin, e.Message);
                chat.FormClosed += delegate(object s, FormClosedEventArgs ee)
                {
                    openedChats.Remove(chat);
                };
                openedChats.Add(chat);
            }));
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                int uin = 0;
                if (int.TryParse(listBox1.SelectedItem.ToString(), out uin))
                {
                    Chat chat = new Chat(uin, protocol);
                    chat.Show();
                    chat.FormClosed += delegate(object s, FormClosedEventArgs ee)
                    {
                        openedChats.Remove(chat);
                    };
                    openedChats.Add(chat);
                }
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            if (protocol != null)
                protocol.Dispose();
        }

        private void dodajKontaktToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addContact ac = new addContact();
            if (ac.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                listBox1.Items.Add(ac.uin);
                protocol.AddUser(new User(ac.uin));
            }
        }

        private void usuńKontaktToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(string.Format("Jesteś pewien, że chcesz usunąć kontakt {0}?", listBox1.SelectedItem), Text, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                protocol.RemoveUser(new User(int.Parse(listBox1.SelectedItem.ToString())));
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                contextMenuStrip1.Show(listBox1, listBox1.PointToScreen(listBox1.PointToClient(e.Location)));
        }

        private void pokażLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Invoke(new Action(delegate()
                {
                    if (!log.IsDisposed)
                        log.Show();
                    else
                    {
                        log = new Log(protocol);
                        log.Show();
                    }
                }));
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            Invoke(new Action(delegate()
                {
                    Status stat = new Status(protocol);
                    stat.ShowDialog();
                }));
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {
            Parallel par = new Parallel(protocol);
            par.ShowDialog();
        }
    }
}
